//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Web.UI;
using NopSolutions.NopCommerce.BusinessLogic.Model;
using NopSolutions.NopCommerce.Common.Utils;

namespace NopSolutions.NopCommerce.Web.Templates.Products
{
    public partial class VariantsInGrid : BaseNopFrontendUserControl
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!IsPostBack) {
                BindData();
            }
        }

        protected void BindData() {
            Product product = this.ProductService.GetProductById(this.ProductId);
            if (product != null) {
                lProductName.Text = Server.HtmlEncode(product.LocalizedName);
                lShortDescription.Text = product.LocalizedShortDescription;
                lFullDescription.Text = product.LocalizedFullDescription;

                //pictures
                var pictures = this.PictureService.GetPicturesByProductId(product.ProductId);
                if (pictures.Count > 1) {
                    defaultImage.ImageUrl = this.PictureService.GetPictureUrl(pictures[0], this.SettingManager.GetSettingValueInteger("Media.Product.DetailImageSize", 300));
                    defaultImage.ToolTip = String.Format(GetLocaleResourceString("Media.Product.ImageAlternateTextFormat"), product.LocalizedName);
                    defaultImage.AlternateText = String.Format(GetLocaleResourceString("Media.Product.ImageAlternateTextFormat"), product.LocalizedName);
                    rptProductPictures.DataSource = pictures;
                    rptProductPictures.DataBind();
                } else if (pictures.Count == 1) {
                    defaultImage.ImageUrl = this.PictureService.GetPictureUrl(pictures[0], this.SettingManager.GetSettingValueInteger("Media.Product.DetailImageSize", 300));
                    defaultImage.ToolTip = String.Format(GetLocaleResourceString("Media.Product.ImageAlternateTextFormat"), product.LocalizedName);
                    defaultImage.AlternateText = String.Format(GetLocaleResourceString("Media.Product.ImageAlternateTextFormat"), product.LocalizedName);
                    rptProductPictures.Visible = false;
                } else {
                    defaultImage.ImageUrl = this.PictureService.GetDefaultPictureUrl(this.SettingManager.GetSettingValueInteger("Media.Product.DetailImageSize", 300));
                    defaultImage.ToolTip = String.Format(GetLocaleResourceString("Media.Product.ImageAlternateTextFormat"), product.LocalizedName);
                    defaultImage.AlternateText = String.Format(GetLocaleResourceString("Media.Product.ImageAlternateTextFormat"), product.LocalizedName);
                    rptProductPictures.Visible = false;
                }
                if (this.SettingManager.GetSettingValueBoolean("Media.Product.DefaultPictureZoomEnabled", false)) {
                    var picture = product.DefaultPicture;
                    if (picture != null) {
                        lnkMainLightbox.Attributes["href"] = this.PictureService.GetPictureUrl(picture);
                        lnkMainLightbox.Attributes["rel"] = "lightbox";
                    }
                }

                //manufacturers
                List<Manufacturer> manufacturers = new List<Manufacturer>();
                foreach (var pm in product.ProductManufacturers) {
                    var manufacturer = pm.Manufacturer;
                    if (manufacturer != null)
                        manufacturers.Add(manufacturer);
                }
                if (manufacturers.Count > 0) {
                    if (manufacturers.Count == 1) {
                        lManufacturersTitle.Text = GetLocaleResourceString("Products.Manufacturer");
                    } else {
                        lManufacturersTitle.Text = GetLocaleResourceString("Products.Manufacturers");
                    }
                    rptrManufacturers.DataSource = manufacturers;
                    rptrManufacturers.DataBind();
                } else {
                    phManufacturers.Visible = false;
                }

                //ctrlProductRating.Visible = product.AllowCustomerRatings;
            }
        }

        protected void Page_PreRender(object sender, EventArgs e) {
            //pnlProductReviews.Visible = ctrlProductReviews.Visible;
            //pnlProductSpecs.Visible = ctrlProductSpecs.Visible;
            //pnlProductTags.Visible = ctrlProductTags.Visible;
            //ProductsTabs.Visible = pnlProductReviews.Visible ||
            //    pnlProductSpecs.Visible ||
            //    pnlProductTags.Visible;

            ////little hack here
            //if (pnlProductTags.Visible)
            //    ProductsTabs.ActiveTab = pnlProductTags;
            //if (pnlProductSpecs.Visible)
            //    ProductsTabs.ActiveTab = pnlProductSpecs;
            //if (pnlProductReviews.Visible)
            //    ProductsTabs.ActiveTab = pnlProductReviews;
        }

        public int ProductId {
            get {
                return CommonHelper.RouteStringInt("productId");
            }
        }
    }
}